import 'dart:math';

import 'package:flutter/material.dart';


class LoadingCanvas extends CustomPainter {
  final double width;
  final double value;

  final double strokeWidth = 3;
  late double center;

  LoadingCanvas({required this.width, required this.value}):
      center = width / 2;

  @override
  void paint(Canvas canvas, Size size) {
    if (value <= 1.0) {
      _square(canvas);
    } else {
      _arcDraw(canvas);
    }
  }

  _square (Canvas canvas) {
    double dx = 0.0 + value * width;
    canvas.drawLine(Offset(dx, 0), Offset(0, dx), Paint()..color = Colors.blue..strokeWidth = 10..strokeCap = StrokeCap.round);
  }

  _arcDraw(Canvas canvas) {
    double leftStartAngle = 1.0 + value / 2 * pi;
    double rightStartAngle = 1.0 - value / 2 * pi;

    canvas.drawPath(
        Path()..arcTo(Rect.fromCircle(center: Offset(center, center), radius: center + strokeWidth), leftStartAngle, 0.5, false),
        Paint()..color = Colors.red
          ..strokeWidth = 3.0
          ..style = PaintingStyle.stroke);
    canvas.drawPath(
        Path()..arcTo(Rect.fromCircle(center: Offset(center, center), radius: center + strokeWidth), rightStartAngle, -0.5, false),
        Paint()..color = Colors.red
          ..strokeWidth = strokeWidth
          ..style = PaintingStyle.stroke);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => true;

}